package com.example.paopao.consumer.memer.alipay.service.impl;

import com.example.paopao.common.enumerator.ServiceCode;
import com.example.paopao.common.exception.ServiceException;
import com.example.paopao.common.pojo.temp.RacheInfo;
import com.example.paopao.consumer.memer.alipay.dao.persist.repository.IPayIntegralRepository;
import com.example.paopao.consumer.memer.alipay.pojo.dto.PayMent;
import com.example.paopao.consumer.memer.alipay.pojo.entity.IntegralRacharge;
import com.example.paopao.consumer.memer.alipay.pojo.po.AccountPO;
import com.example.paopao.consumer.memer.alipay.service.PayService;
import com.example.paopao.consumer.memer.dao.cache.IIntegralsCacheRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private IPayIntegralRepository repository;

    @Autowired
    private IIntegralsCacheRepository integralsCacheRepository;

    @Override
    public int insertPay(PayMent payMent) {

        IntegralRacharge integralRacharge = new IntegralRacharge();

        String key = payMent.getOut_trade_no().toString();
        RacheInfo racheInfo = new RacheInfo();
        /*
        map.put("userId",racheInfo.getUserId());
        map.put("subject",racheInfo.getSubject());
        map.put("totalAmount",racheInfo.getTotalAmount());
        map.put("outTradeNo",racheInfo.getOutTradeNo());*/
        System.out.println(integralsCacheRepository);
        Object userId = integralsCacheRepository.getOrder(key, "userId");
        if (userId==null){
            String message="积分订单错误，请稍后重试！";
            throw new ServiceException(ServiceCode.ERROR_UNAUTHORIZED,message);
        }
        racheInfo.setUserId(Long.valueOf(userId.toString()));
        racheInfo.setSubject(integralsCacheRepository.getOrder(key, "subject").toString());
        racheInfo.setTotalAmount(BigDecimal.valueOf(Long.valueOf(integralsCacheRepository.getOrder(key,"totalAmount").toString())));
        racheInfo.setOutTradeNo(integralsCacheRepository.getOrder(key,"outTradeNo").toString());

        integralRacharge.setUserId(racheInfo.getUserId());
        integralRacharge.setAmountM(racheInfo.getTotalAmount());
        integralRacharge.setOrderN(racheInfo.getOutTradeNo());
        integralRacharge.setCreateTime(LocalDateTime.now());
        integralRacharge.setIsTransac(true);
        BigDecimal val = BigDecimal.valueOf(Double.valueOf(payMent.getTotal_amount().toString()));
        integralRacharge.setMAmount(val);
        integralRacharge.setMId(racheInfo.getUserId());
        integralRacharge.setMTime(LocalDateTime.now());
        integralRacharge.setStansacName(racheInfo.getSubject());
        integralRacharge.setVoucherN(payMent.getTrade_no().toString());


        int insertIntegral = repository.insertIntegral(integralRacharge);
        AccountPO accountPO=new AccountPO();
        accountPO.setUserId(racheInfo.getUserId());
        Integer integral = Integer.valueOf(integralsCacheRepository.getOrder(key, "integral").toString());
        accountPO.setBalance(integral);
        repository.updateAccount(accountPO);
        if (insertIntegral==1){
            integralsCacheRepository.deleteOrder(integralRacharge.getOrderN());
        }

        return insertIntegral;
    }
}
